package org.csu.mypetstore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.csu.mypetstore.commonResponse.CommonResponse;
import org.csu.mypetstore.domin.SignOn;
import org.csu.mypetstore.persistence.SignOnMapper;
import org.csu.mypetstore.service.SignOnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("SignOnService")
public class SignOnServiceImpl implements SignOnService {

    @Autowired
    private SignOnMapper signOnMapper;
    @Override
    public CommonResponse<SignOn> getSignOnByUsernameAndPassword(SignOn signOn) {
        QueryWrapper<SignOn> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",signOn.getUsername()).eq("password",signOn.getPassword());
        SignOn signOnTemp=signOnMapper.selectOne(queryWrapper);
        if(signOnTemp==null){
            return CommonResponse.createForError("通过username和password无法找到该用户");
        }
        return CommonResponse.createForSuccess("查找成功",signOn);
    }

    @Override
    public CommonResponse<SignOn> insertSignOn(SignOn signOn) {
        int y=signOnMapper.insert(signOn);
        if(y==0){
            return CommonResponse.createForError("服务器内部异常");
        }
        return CommonResponse.createForSuccess("新增用户成功",signOn);
    }

    @Override
    public CommonResponse<SignOn> updateSignOn(SignOn signOn) {
        UpdateWrapper<SignOn> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("username",signOn.getUsername());
        int y=signOnMapper.update(signOn,updateWrapper);
        if(y==0){
            return CommonResponse.createForError("服务器内部异常");
        }
        return CommonResponse.createForSuccess("更新用户成功",signOn);
    }
}
